package cn.llming.org.controller;


import cn.llming.auth.util.BasePermission;
import cn.llming.basic.query.BaseQuery;
import cn.llming.basic.result.BaseResult;
import cn.llming.org.domain.Department;
import cn.llming.org.query.DepartmentQuery;
import cn.llming.org.service.IDepartmentService;
import cn.llming.vo.result.AjaxResult;
import cn.llming.vo.result.ListPager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/department")
@Api(value = "部门接口管理",description="部门管理接口")
@BasePermission(name = "部门管理",desc = "部门管理权限")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    //增加/编辑
    @PutMapping
    @ApiOperation(value = "增加或者修改接口")
    @BasePermission(name = "增加或者编辑接口",desc = "增加或者编辑接口")
    public AjaxResult addAndUpdate(@RequestBody Department department){
        try {
            if(Objects.nonNull(department.getId())){
               departmentService.update(department);
            }else {
                departmentService.insert(department);
            }

            return AjaxResult.myRes();
        }catch (Exception e){
            return AjaxResult.myRes().setMessage("操作失败"+e.getMessage()).setSuccess(false);
        }

    }

    //删除
    @ApiOperation(value = "删除接口")
    @DeleteMapping("/{id}")
    @BasePermission(name = "删除接口",desc = "删除接口")
    public AjaxResult delete(@ApiParam(value = "需要对应部门id",required = true)@PathVariable("id") Long id){

        try {

            departmentService.delete(id);
            return AjaxResult.myRes();
        }catch (Exception e){
            return AjaxResult.myRes().setMessage("删除失败"+e.getMessage()).setSuccess(false);
        }
    }
    //查询单个
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个数据接口")
    @BasePermission(name = "查询单个数据接口",desc = "查询单个数据接口")
    public AjaxResult getById(@PathVariable("id") Long id){

        try {

            Department department = departmentService.getById(id);

            return AjaxResult.myRes().setObject(department);
        }catch (Exception e){
            return AjaxResult.myRes().setMessage("查询失败"+e.getMessage()).setSuccess(false);
        }
    }

    //查询全部
    @GetMapping
    @ApiOperation(value = "查询全部接口")
    @BasePermission(name = "查询全部接口",desc = "查询全部接口")
    public AjaxResult getAll(){
        try {
            List<Department> departments = departmentService.getAll();
            return AjaxResult.myRes().setObject(departments);
        }catch (Exception e){
            return AjaxResult.myRes().setMessage("删除失败"+e.getMessage()).setSuccess(false);
        }
    }

    //分页查询

    @PostMapping
    @BasePermission(name = "分页查询",desc = "分页查询")
    public AjaxResult getPage(@RequestBody ListPager query){
        try {
            BaseResult<Department> query1 = departmentService.getPageList(query);
            return AjaxResult.myRes().setObject(query1);
        }catch (Exception e){
            return AjaxResult.myRes().setMessage("分页查询失败"+e.getMessage()).setSuccess(false);
        }

    }
    //批量删除

    @PatchMapping
    @BasePermission(name = "批量删除",desc = "批量删除")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.batchDelete(ids);
            return AjaxResult.myRes();
        }catch (Exception e){
            return AjaxResult.myRes().setMessage("批量删除失败"+e.getMessage()).setSuccess(false);
        }

    }


    //部门2级查询
    @GetMapping("/tree")
    @ApiOperation(value = "查询全部接口")
    @BasePermission(name = "查询全部接口",desc = "查询全部接口")
    public AjaxResult tree(){
        try {
            List<Department> departments = departmentService.getDeptTree();
            return AjaxResult.myRes().setObject(departments);
        }catch (Exception e){
            return AjaxResult.myRes().setMessage("查询失败"+e.getMessage()).setSuccess(false);
        }
    }
}
